package com.li.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.pojo.Permission;
import com.li.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionsController {

    @Reference
    PermissionsService permissionsService;

    //获取所有权限
    @RequestMapping("/findAll")
    public Result findAll(HttpServletRequest request) {
        request.setAttribute("message", "");
        List<Permission> list = permissionsService.findAll();
        return new Result(true, "获取权限数据成功", list);
    }

    // 增加
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission, HttpServletRequest request) {
        request.setAttribute("message", "");
        permissionsService.add(permission);
        return new Result(true, "");
    }

    // 删除
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id, HttpServletRequest request) {
        request.setAttribute("message", "");
        permissionsService.deleteById(id);
        return new Result(true, "");
    }

    // 修改
    @RequestMapping("/updateById")
    public Result updateById(@RequestBody Permission permission, HttpServletRequest request) {
        request.setAttribute("message", "");
        permissionsService.updateById(permission);
        return new Result(true, "");
    }

    // 修改时回显数据
    @RequestMapping("/findById")
    public Result findById(Integer id, HttpServletRequest request) {
        request.setAttribute("message", "");
        Permission permission = permissionsService.findById(id);
        return new Result(true, "", permission);
    }


    // 查询(带条件)
    @RequestMapping("/findPermissions")
    public Result findPermissions(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request) {
        request.setAttribute("message", "");
        PageResult pageResult = permissionsService.findAll(queryPageBean);
        return new Result(true, "", pageResult);
    }

    //查询角色对应的所有权限ids
    @RequestMapping("/findIdsByRoleId")
    public Result findIdsByRoleId(Integer roleId, HttpServletRequest request) {
        request.setAttribute("message", "");
        List<Integer> ids = permissionsService.findIdsByRoleId(roleId);
        return new Result(true, "", ids);
    }

}
